{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37612e3d",
   "metadata": {},
   "source": [
    "## Spine Manual Classification Notebook "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93a38608",
   "metadata": {},
   "source": [
    "Input file paths."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "800fcf46",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_path = \"example_dendrite\"\n",
    "metrics_path = f\"{dataset_path}/metrics.csv\"\n",
    "\n",
    "scale = (1, 1, 1)\n",
    "\n",
    "classification_save_path = \"output/manual_classification.json\"\n",
    "\n",
    "classes = [\"Stubby\", \"Mushroom\", \"Thin\", \"Filopodia\", \"Outlier\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0145cae4",
   "metadata": {},
   "source": [
    "Load spine meshes and metrics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd73c7ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "from spine_metrics import SpineMetricDataset\n",
    "from notebook_widgets import SpineMeshDataset\n",
    "from spine_segmentation import apply_scale\n",
    "\n",
    "# load meshes\n",
    "spine_dataset = SpineMeshDataset().load(dataset_path)\n",
    "\n",
    "# apply scale along z axis\n",
    "spine_dataset.apply_scale(scale)\n",
    "\n",
    "# load metrics\n",
    "spine_metrics = SpineMetricDataset().load(metrics_path)\n",
    "spine_metrics = spine_metrics.get_spines_subset(spine_dataset.spine_meshes.keys())\n",
    "spine_metrics = spine_metrics.get_metrics_subset(['OldChordDistribution'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "363016be",
   "metadata": {},
   "source": [
    "Perform manual classification."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3aa07bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebook_widgets import manual_classification_widget\n",
    "from spine_fitter import SpineGrouping\n",
    "\n",
    "# try to load classification\n",
    "init = SpineGrouping()\n",
    "try:\n",
    "    init.load(classification_save_path)\n",
    "except FileNotFoundError:\n",
    "    init = None\n",
    "\n",
    "manual_classification = manual_classification_widget(spine_dataset, spine_metrics, classes, init)\n",
    "display(manual_classification)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dc9059d",
   "metadata": {},
   "source": [
    "Save classification."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4548cd7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebook_widgets import create_dir\n",
    "\n",
    "create_dir(\"output\")\n",
    "classification = manual_classification.children[1].result\n",
    "display(classification.show(spine_metrics))\n",
    "classification.save(classification_save_path)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
